package com.fivefu.core.camera.services.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fivefu.base.common.utils.date.DateUtils;
import com.fivefu.base.common.utils.str.StrUtils;
import com.fivefu.core.camera.constant.StatusEnum;
import com.fivefu.core.camera.entity.*;
import com.fivefu.core.camera.mapper.SceneEventMapper;
import com.fivefu.core.camera.services.*;
import com.fivefu.core.camera.vo.EventStatusVo;
import com.fivefu.core.camera.vo.request.*;
import com.fivefu.core.camera.vo.response.EventCameraBean;
import com.fivefu.core.camera.vo.response.EventExamineBean;
import com.fivefu.core.camera.vo.response.SceneCameraBean;
import com.fivefu.core.skyeye.event.entity.DbAiEvent;
import com.fivefu.core.skyeye.event.entity.DbAiEventImage;
import com.fivefu.core.skyeye.event.entity.DbAiScene;
import com.fivefu.core.skyeye.event.entity.DbAiSceneCamera;
import com.fivefu.core.skyeye.event.services.*;
import com.fivefu.core.skyeye.event.vo.SceneCameraVo;
import com.fivefu.core.skyeye.event.vo.SceneVo;
import com.fivefu.module.dictionary.entity.DbSysDict;
import com.fivefu.module.dictionary.service.DbSysDictService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class SceneEventServiceImpl implements SceneEventService {

    private Logger logger = LoggerFactory.getLogger(SceneEventServiceImpl.class);

    @Autowired
    private SceneEventMapper sceneEventMapper;

    @Resource
    private SceneService sceneService;
    @Resource
    private DbAiSceneService dbAiSceneService;
    @Resource
    private DbAiSceneCameraService dbAiSceneCameraService;

    @Autowired
    private DbSceneEventService dbSceneEventService;
    @Autowired
    private EventTypeService eventTypeService;
    @Autowired
    private DbMonitorLabelService dbMonitorLabelService;
    @Autowired
    private DbMonitorCameraLabelService dbMonitorCameraLabelService;
    @Autowired
    private DbMonitorCameraService dbMonitorCameraService;
    @Autowired
    private RegionService regionService;
    @Autowired
    private DbAiEventService dbAiEventService;
    @Autowired
    private DbMonitorEventExamineService dbMonitorEventExamineService;
    @Autowired
    private DbMonitorWorkorderService dbMonitorWorkorderService;
    @Autowired
    private DbSysDictService dbSysDictService;
    @Autowired
    private DbAiEventImageService dbAiEventImageService;
    @Autowired
    private DbMonitorWorkorderImageService dbMonitorWorkorderImageService;

    /**
     * 编辑场景
     * @param sceneVo 场景信息
     * @param dbSceneEventVo 场景生成工单信息
     * @param userid 操作用户编号
     */
    @Override
    public boolean editSceneEvent(SceneVo sceneVo, DbSceneEventVo dbSceneEventVo, String userid){
        Long scenId = sceneService.editScene(sceneVo, userid);
        if (scenId != null){
            DbAiScene scene = dbAiSceneService.querySceneByID(scenId);
            if (scene != null){
                String code = scene.getCode();
                if (StrUtils.isNotNull(code)){
                    List<Long> sceneEventIds = new ArrayList<>();
                    //数字城管工单
                    if (StrUtils.isNotNull(dbSceneEventVo.getOrderType())) {
                        List<DbSceneEvent> sceneEventList = dbSceneEventService.querySceneEventBySceneCode(code, "数字城管");

                        DbSceneEvent event = new DbSceneEvent();
                        event.setCode(code);//场景标识
                        event.setOrderType(dbSceneEventVo.getOrderType());//问题类型
                        event.setOrderBigtype(dbSceneEventVo.getOrderBigtype());//问题大类
                        event.setOrderSmalltype(dbSceneEventVo.getOrderSmalltype());//问题小类

                        if (StrUtils.isNotNull(dbSceneEventVo.getOrderType())){
                            event.setEventType(eventTypeService.queryEventTypeCodeByCode(dbSceneEventVo.getOrderType()));
                        }
                        if (StrUtils.isNotNull(dbSceneEventVo.getOrderBigtype())){
                            event.setEventBigtype(eventTypeService.queryEventTypeCodeByCode(dbSceneEventVo.getOrderBigtype()));
                        }
                        if (StrUtils.isNotNull(dbSceneEventVo.getOrderSmalltype())){
                            event.setEventSmalltype(eventTypeService.queryEventTypeCodeByCode(dbSceneEventVo.getOrderSmalltype()));
                        }

                        event.setEventImageNum(dbSceneEventVo.getEventImageNum());//图片数量
                        event.setEventDescription(dbSceneEventVo.getEventDescription());//工单描述
                        event.setType("数字城管");//工单类型
                        if (sceneEventList.size() > 0) {
                            event.setId(sceneEventList.get(0).getId());
                        }
                        Long sceneEventId = dbSceneEventService.editSceneEvent(event, userid);
                        sceneEventIds.add(sceneEventId);
                    }
                    //删除多余的场景工单配置信息
                    dbSceneEventService.removeSurplusSceneEvent(code,sceneEventIds,userid);
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 查询场景详情
     * @param id 场景编号
     * @return
     */
    @Override
    public Map<String, Object> querySceneEvent(Long id){
        //查询场景详情
        Map<String, Object> sceneMap = dbAiSceneService.querySceneDetail(id);
        if (sceneMap != null && sceneMap.containsKey("code")){
            String code = sceneMap.get("code").toString();
            if (StrUtils.isNotNull(code)) {
                List<Map<String,Object>> eventSetup = new ArrayList<>();
                List<DbSceneEvent> list = dbSceneEventService.querySceneEventBySceneCode(code, null);
                list.forEach(event -> {
                    Map<String, Object> sceneEventMap = dbSceneEventService.formatSceneEvent(event);
                    eventSetup.add(sceneEventMap);
                });
                sceneMap.put("eventSetup",eventSetup);
            }
        }
        return sceneMap;
    }

    /**
     * 查询场景下探头列表
     * @param sceneId 场景编号
     * @param page 页码
     * @param limit 数量
     * @return
     */
    @Override
    public Page<SceneCameraBean> querySceneCamera(Long sceneId, Integer page, Integer limit){
        Page<SceneCameraBean> returnPage = new Page<>();
        DbAiScene scene = dbAiSceneService.querySceneByID(sceneId);
        if (scene != null){
            String code = scene.getCode();
            IPage<SceneCameraBean> iPage = new Page<>(page,limit);
            returnPage = sceneEventMapper.querySceneCameraList(iPage,code);

            List<SceneCameraBean> list = returnPage.getRecords();
            list.forEach(bean -> {
                if (StrUtils.isNotNull(bean.getCameraType())){
                    DbSysDict dict = dbSysDictService.queryDictDetail(null, bean.getCameraType());
                    if (dict != null) {
                        bean.setCameraTypeName(dict.getDictName());
                    }
                }
                if (StrUtils.isNotNull(bean.getCameraManufacturer())){
                    DbSysDict dict = dbSysDictService.queryDictDetail(null, bean.getCameraManufacturer());
                    if (dict != null) {
                        bean.setCameraManufacturerName(dict.getDictName());
                    }
                }
                if (StrUtils.isNotNull(bean.getAreaid())){
                    String regionName = regionService.queryAreaNameByCode(bean.getAreaid());
                    bean.setAreaName(regionName);
                }
            });
        }
        return returnPage;
    }

    /**
     * 查询增加探头列表
     * @param sceneId 探头编号
     * @param cameraName 探头名称(模糊)
     * @param cameraCode 探头标识(模糊)
     * @param labelName 标签名称
     * @param areaid 区域编号
     * @param streetid 所属街道
     * @param cameraManufacturer 厂商
     * @param page 页码
     * @param limit 数量
     * @return
     */
    @Override
    public Page<SceneCameraBean> queryAddCamera(Long sceneId, String cameraName, String cameraCode, String labelName, String areaid,String streetid,
                                                String cameraManufacturer, String cameraStatus, String onlineStatus, Integer page, Integer limit){
        Page<SceneCameraBean> returnPage = new Page<>();
        List<String> excludeCodeList = new ArrayList<>();
        List<String> codeList = new ArrayList<>();
        DbAiScene scene = dbAiSceneService.querySceneByID(sceneId);
        if (scene != null) {
            String code = scene.getCode();
            excludeCodeList = dbAiSceneCameraService.queryCameraCodeBySceneID(code, null);
        }
        if (StrUtils.isNotNull(labelName)){
            List<Long> labelIds = dbMonitorLabelService.queryLabelIdsByLabelName(labelName);
            if (labelIds == null || labelIds.isEmpty()){
                return returnPage;
            }
            Set<String> codeSet = dbMonitorCameraLabelService.queryMonitorCameraByLabelIds(labelIds);
            if (codeSet == null || codeSet.size() == 0){
                return returnPage;
            }
            codeList = codeSet.stream().collect(Collectors.toList());
        }
        Page<DbMonitorCamera> cameraPage = dbMonitorCameraService.queryCameraList(cameraName, cameraCode,null, areaid, streetid,cameraStatus, onlineStatus, cameraManufacturer, codeList, excludeCodeList, page, limit);
        List<SceneCameraBean> beanList = new ArrayList<>();
        cameraPage.getRecords().forEach(camera -> {
            SceneCameraBean bean = new SceneCameraBean();
            bean.setCameraId(camera.getId());
            bean.setCameraName(camera.getCameraName());
            bean.setCameraCode(camera.getCameraCode());
            bean.setCameraManufacturer(camera.getCameraManufacturer());
            //所属城区
            if (StrUtils.isNotNull(camera.getCameraAreaid())) {
                String regionCode = camera.getCameraAreaid();
                bean.setAreaid(regionCode);
                String areaName = regionService.queryAreaNameByCode(regionCode);
                bean.setAreaName(areaName);
            }
            bean.setOnlineStatus(camera.getOnlineStatus());
            bean.setCameraStatus(camera.getCameraStatus());
            beanList.add(bean);
        });

        returnPage.setTotal(cameraPage.getTotal());
        returnPage.setRecords(beanList);

        return returnPage;
    }

    /**
     * 查询事件下案卷列表
     * @param sceneCode 场景标识
     * @param areaid 所属区域
     * @param cameraName 探头名称
     * @param onlineStatus 在线状态
     * @param sStime 开始时间
     * @param sEtime 开始时间
     * @param eStime 结束时间
     * @param eEtime 结束时间
     * @param queryType 查询类型(1:待审核, 2:审核不合格, 不传值:全部)
     * @param page 页码
     * @param limit 数量
     * @return
     */
    @Override
    public Page<EventCameraBean> queryEventList(String sceneCode, String areaid, String cameraName, String onlineStatus,
                                         String sStime, String sEtime, String eStime, String eEtime, Integer queryType, Integer page, Integer limit){

        Page<EventCameraBean> backPage = new Page<>();
        List<Long> eventIds = new ArrayList<>();
        List<Long> excludeeventIds = new ArrayList<>();

        if (queryType != null){
            Long status = null;
            if (queryType.equals(2)){
                status = StatusEnum.EXAMINE_UNHEGE.getStatusId();
            }
            List<Long> list = dbMonitorEventExamineService.queryEventIdByExamineStatus(status);

            if (status == null){//待审核，排除所有审核过的事件编号
                excludeeventIds = list;
            }else {//审核不合格，保留所有审核不合格的事件编号
                if (list == null || list.isEmpty()){//没有审核不合格的事件
                    return backPage;
                }
                eventIds = list;
            }
        }

        IPage<EventCameraBean> iPage = new Page<>(page, limit);
        backPage = sceneEventMapper.queryEventList(iPage, sceneCode, areaid, cameraName, onlineStatus, sStime, sEtime, eStime, eEtime,eventIds,excludeeventIds);
        backPage.getRecords().forEach(bean -> {
            //所属城区
            if (bean.getAreaid() != null){
                String areaName = regionService.queryAreaNameByCode(bean.getAreaid());
                bean.setAreaName(areaName);
            }
            //场景名称
            if (StrUtils.isNotNull(bean.getSceneCode())){
                DbAiScene scene = dbAiSceneService.querySceneByCode(bean.getSceneCode());
                if (scene != null){
                    bean.setSceneName(scene.getName());
                }
            }
        });
        return backPage;
    }

    /**
     * 查询事件详情
     * @param eventId 事件编号
     * @return
     */
    @Override
    public Map<String, Object> queryEventDetail(Long eventId){
        Map<String, Object> eventMap = dbAiEventService.getEventInfo(eventId);
        DbMonitorEventExamine examine = dbMonitorEventExamineService.queryExamineByEventId(eventId);
        if (examine != null){
            Map<String, Object> examineMap = dbMonitorEventExamineService.formatEventExamine(examine);
            eventMap.put("examine",examineMap);
        }
        return eventMap;
    }

    /**
     * 拟生成工单模板
     * @param eventId 事件编号
     * @return
     */
    @Override
    public EventExamineBean createOrderTemplate(Long eventId){
        DbAiEvent event = dbAiEventService.queryEventById(eventId);
        DbAiScene scene = null;
        if (event != null) {
            EventExamineBean bean = new EventExamineBean();
            bean.setEventId(event.getId());//事件编号

            String sceneCode = event.getSceneId();
            String cameraCode = event.getCameraId();
            if (StrUtils.isNotNull(sceneCode)) {
                List<DbSceneEvent> sceneEventList = dbSceneEventService.querySceneEventBySceneCode(sceneCode, "数字城管");
                if (sceneEventList != null && sceneEventList.size() > 0) {
                    DbSceneEvent sceneEvent = sceneEventList.get(0);

                    bean.setOrderType(sceneEvent.getOrderType());//问题类型
                    bean.setOrderBigType(sceneEvent.getOrderBigtype());//问题大类
                    bean.setOrderSmallType(sceneEvent.getOrderSmalltype());//问题小类
                    bean.setImgNum(sceneEvent.getEventImageNum());//图片数量
                    if (StrUtils.isNotNull(sceneEvent.getOrderType())){
                        bean.setOrderTypeName(eventTypeService.queryEventTypeNameByCode(sceneEvent.getOrderType()));
                    }
                    if (StrUtils.isNotNull(sceneEvent.getOrderBigtype())){
                        bean.setOrderBigTypeName(eventTypeService.queryEventTypeNameByCode(sceneEvent.getOrderBigtype()));
                    }
                    if (StrUtils.isNotNull(sceneEvent.getOrderSmalltype())){
                        bean.setOrderSmallTypeName(eventTypeService.queryEventTypeNameByCode(sceneEvent.getOrderSmalltype()));
                    }
                    if (StrUtils.isNotNull(sceneEvent.getEventDescription())){
                        bean.setOrderDescription(sceneEvent.getEventDescription());
                    }
                }

                scene = dbAiSceneService.querySceneByCode(sceneCode);
            }
            if (StrUtils.isNotNull(cameraCode)){
                DbMonitorCamera camera = dbMonitorCameraService.queryMonitorCameraByCode(cameraCode);
                if (camera != null){
                    bean.setOrderAreaid(camera.getCameraAreaid());//所属城区
                    bean.setOrderStreetid(camera.getCameraStreetid());//所属街道
                    bean.setCameraCode(camera.getCameraCode());//探头编号
                    bean.setCameraName(camera.getCameraName());//探头名称
                    bean.setOnlineStatus(camera.getOnlineStatus());//探头状态
                    bean.setOrderLatitude(camera.getCameraLatitude());//纬度
                    bean.setOrderLongitude(camera.getCameraLongitude());//经度
                    bean.setOrderAddress(camera.getCameraAddress());//地址
                    if (StrUtils.isNotNull(camera.getCameraAreaid())){
                        bean.setOrderAreaName(regionService.queryAreaNameByCode(camera.getCameraStreetid()));
                    }
                    if (StrUtils.isNotNull(camera.getCameraStreetid())){
                        bean.setOrderStreetName(regionService.queryStreetNameByCode(camera.getCameraAreaid(),camera.getCameraStreetid()));
                    }
                }
            }

            String desc = bean.getOrderDescription();
            if (StrUtils.isNotNull(desc)) {
                String[] descName = new String[]{"工单类型", "工单大类", "工单小类", "地址", "探头名称", "事件发生时间", "场景名称"};
                String[] descValue = new String[descName.length];
                descValue[0] = bean.getOrderTypeName();
                descValue[1] = bean.getOrderBigTypeName();
                descValue[2] = bean.getOrderSmallTypeName();
                descValue[3] = bean.getOrderAddress();
                descValue[4] = bean.getCameraName();
                if (event.getStartTime() != null) {
                    descValue[5] = DateUtils.format(event.getStartTime(), DateUtils.DEFAULT_DATETIME_PATTERN);
                }
                if (scene != null) {
                    descValue[6] = scene.getName();
                }

                for (int i = 0 ; i < descName.length ; i ++) {
                    String name = descName[i];
                    if (desc.contains("【" + name + "】")){
                        String value = descValue[i] == null ? "" : descValue[i];
                        desc = desc.replaceAll("【" + name + "】", value);
                    }
                }
                bean.setOrderDescription(desc);
            }


            return bean;
        }
        return null;
    }

    /**
     * 事件审核
     * @param examineVo 审核信息
     * @param workorderVo 工单信息
     * @param userid 操作用户编号
     */
    @Override
    public Long examineEvent(EventExamineVo examineVo, WorkorderVo workorderVo, String userid){
        Long orderId = null;
        if (examineVo.getExamineStatus().equals(StatusEnum.EXAMINE_HEGE.getStatusId())){//合格
            //生成工单
            orderId = createWorkorder(workorderVo, userid);
        }

        //审核信息
        DbMonitorEventExamine examine = new DbMonitorEventExamine();
        examine.setEventId(workorderVo.getEventId());//事件编号
        examine.setExamineReason(examineVo.getExamineReason());//理由
        examine.setExamineStatus(examineVo.getExamineStatus());//审核状态
        examine.setExamineUser(examineVo.getExamineUser());//审核人
        examine.setExamineTime(examineVo.getExamineTime());//审核时间
        dbMonitorEventExamineService.examineEvent(examine,userid);

        return orderId;
    }

    /**
     * 生成工单
     * @param workorderVo 工单信息
     * @param userid 操作用户编号
     * @return
     */
    @Override
    public Long createWorkorder(WorkorderVo workorderVo, String userid){
        //获取图片信息
        List<DbMonitorWorkorderImage> images = new ArrayList<>();
        if (StrUtils.isNotNull(workorderVo.getImages())){
            images = JSON.parseArray(workorderVo.getImages(),DbMonitorWorkorderImage.class);
            for (DbMonitorWorkorderImage image : images) {
                image.setFileType("report");
            }
        }
        /*List<DbMonitorWorkorderImage> images = new ArrayList<>();
        if (workorderVo.getImageArr() != null && workorderVo.getImageArr().length > 0){
            List<Long> imageIDs = Arrays.stream(Optional.ofNullable(workorderVo.getImageArr()).orElseGet(() -> {
                return null;
            })).collect(Collectors.toList());
            List<DbAiEventImage> imagelist = dbAiEventImageService.getAiEventImageByEventId(workorderVo.getEventId(), imageIDs);
            for (DbAiEventImage dbAiEventImage : imagelist) {
                DbMonitorWorkorderImage image = new DbMonitorWorkorderImage();
                image.setOrderId(orderId);
                image.setFileTime(dbAiEventImage.getInterceptTime());
                image.setFileUrl(dbAiEventImage.getImageUrl());
                image.setMediaType(dbAiEventImage.getImageType());
                image.setFileType("report");
                images.add(image);
            }
        }*/

        DbMonitorWorkorder workorder = new DbMonitorWorkorder();
        workorder.setEventId(workorderVo.getEventId());//事件编号
        workorder.setCameraCode(workorderVo.getCameraCode());//探头编号
        workorder.setCameraName(workorderVo.getCameraName());//探头名称
        workorder.setOrderAreaid(workorderVo.getOrderAreaid());//所属城区
        workorder.setOrderStreetid(workorderVo.getOrderStreetid());//所属街道
        workorder.setOrderType(workorderVo.getOrderType());//问题类型
        workorder.setOrderBigType(workorderVo.getOrderBigType());//问题大类
        workorder.setOrderSmallType(workorderVo.getOrderSmallType());//问题小类
        workorder.setOrderCreateTime(workorderVo.getOrderCreateTime());//生成时间
        workorder.setOrderCreateUser(workorderVo.getOrderCreateUser());//生成人
        workorder.setOrderDescription(workorderVo.getOrderDescription());//问题描述
        workorder.setOrderAddress(workorderVo.getOrderAddress());//详细地址
        workorder.setOrderLatitude(workorderVo.getOrderLatitude());//纬度
        workorder.setOrderLongitude(workorderVo.getOrderLongitude());//经度
        workorder.setOrderStatus(workorderVo.getOrderStatus());//案卷状态
        workorder.setOrderPushStatus(workorderVo.getOrderPushStatus());//推送状态
//        workorder.setOrderStatus(StatusEnum.EVENT_LIAN.getStatusId());//案卷状态
//        workorder.setOrderPushStatus(StatusEnum.PUSH_DAI.getStatusId());//推送状态
        workorder.setType(workorderVo.getType());//工单类型(1:智能工单,2:人工工单)

        if (StrUtils.isNotNull(workorderVo.getOrderType())){
            workorder.setEventType(eventTypeService.queryEventTypeNameByCode(workorderVo.getOrderType()));
        }
        if (StrUtils.isNotNull(workorderVo.getOrderBigType())){
            workorder.setEventBigType(eventTypeService.queryEventTypeNameByCode(workorderVo.getOrderBigType()));
        }
        if (StrUtils.isNotNull(workorderVo.getOrderSmallType())){
            workorder.setEventSmallType(eventTypeService.queryEventTypeNameByCode(workorderVo.getOrderSmallType()));
        }
        Long orderId = dbMonitorWorkorderService.editMonitorWorkorder(workorder, images, userid);
        return orderId;
    }

    /**
     * 批量增加场景下探头
     * @param cameraCode 探头唯一标识（用逗号隔开）
     * @param dbSceneCameraVo 场景探头统一配置信息
     * @param userid 操作用户编号
     */
    @Override
    public boolean addSceneCamera(String cameraCode, DbSceneCameraVo dbSceneCameraVo, String userid) {
        SceneCameraVo sceneCameraVo = new SceneCameraVo();
        sceneCameraVo.setSceneId(dbSceneCameraVo.getSceneId());//场景唯一标识
        sceneCameraVo.setCameraValidTime(dbSceneCameraVo.getCameraValidTime());//探头有效时长
        sceneCameraVo.setCvParam(dbSceneCameraVo.getCvParam());//特殊识别参数值
        sceneCameraVo.setEnable(dbSceneCameraVo.getEnable());//是否开启(0:否 1:是)
        sceneCameraVo.setScope(dbSceneCameraVo.getScope());//是否限定区域;0:否,1:是
        sceneCameraVo.setScopeArea(dbSceneCameraVo.getScopeArea());//区域
        /*sceneCameraVo.setSliceNode(dbSceneCameraVo.getSliceNode());//切片编号
        sceneCameraVo.setEventNode(dbSceneCameraVo.getEventNode());//识别编号*/
        sceneCameraVo.setImage(dbSceneCameraVo.getImage());//缩略图
        //sceneCameraVo.setUserid(userid);//创建人

        List<String> cameras = new ArrayList<>();
        Integer size = null;
        if (StrUtils.isNotNull(cameraCode)){
            cameras  = Arrays.asList(cameraCode.split(","));
            size = cameras.size();
        }
        Long sliceNode = sceneService.getSliceNode(size);//获取切片编号
        Long eventNode = sceneService.getEventNode(size);//获取识别编号
        if (sliceNode != null && eventNode != null){
            sceneCameraVo.setSliceNode(sliceNode);//切片编号
            sceneCameraVo.setEventNode(eventNode);//识别编号
            for (String camera : cameras) {
                sceneCameraVo.setCameraId(camera);//探头唯一标识
                //新增
                sceneService.editSceneCamera(sceneCameraVo, userid);
            }
            return true;
        }
        return false;
    }


    /**
     * 批量移除场景下探头
     * @param sceneId 场景编号
     * @param cameraCode 探头唯一标识(用逗号隔开)
     * @param userid 操作用户
     */
    @Override
    public void removeSceneCamera(Long sceneId, String cameraCode, String userid) {
        List<String> cameras = new ArrayList<>();
        if (StrUtils.isNotNull(cameraCode)){
            cameras  = Arrays.asList(cameraCode.split(","));
        }
        //查询场景
        DbAiScene dbAiScene = dbAiSceneService.querySceneByID(sceneId);
        for (String camera : cameras) {
            //查询场景下探头
            DbAiSceneCamera dbAiSceneCamera = dbAiSceneCameraService.querySceneCamera(dbAiScene.getCode(), camera);
            //移除场景下探头
            sceneService.deleteSceneCamera(dbAiSceneCamera.getId(),userid);
        }

    }

    /**
     *同步生成工单数据
     */
    @Override
    public void synExamineEventOrder(SynEventWorkorderService synEventWorkorderService, EventStatusVo eventStatusVo) {
        List<Long> orderIds = new ArrayList<>();
        //查询所有事件数据信息
        List<DbAiEvent> eventList = dbAiEventService.queryEventList();
        //查询已经生成工单的数据信息
        List<DbMonitorWorkorder> monitorWorkorderList = dbMonitorWorkorderService.queryWorkorderList();
        List<Long> eventIds = monitorWorkorderList.stream().map(k -> k.getEventId()).collect(Collectors.toList());
        if (null != eventList && eventList.size() > 0) {
            for (DbAiEvent event : eventList) {
                if (eventIds != null && !eventIds.isEmpty() && eventIds.contains(event.getId())){
                    continue;
                }
                Long orderId = extracted(event, eventStatusVo);
                orderIds.add(orderId);
            }
        }
        if (synEventWorkorderService != null && orderIds != null && !orderIds.isEmpty()){
            synEventWorkorderService.synEventWorkorder(orderIds);
        }
    }

    /**
     * 抽离工单生成
     * @param event
     */
    private Long extracted(DbAiEvent event, EventStatusVo eventStatusVo) {
        Integer imageNum = null;
        DbMonitorWorkorder workorder = new DbMonitorWorkorder();
        workorder.setEventId(event.getId());//事件编号
        workorder.setType(1);//工单类型(1:智能工单,2:人工工单)
        workorder.setOrderCreateTime(event.getCreatedTime());//生成时间
        workorder.setOrderCreateUser(event.getCreatedBy());//生成人
        if (StrUtils.isNotNull(event.getCameraId())) {
            workorder.setCameraCode(event.getCameraId());//探头编号
            DbMonitorCamera camera = dbMonitorCameraService.queryMonitorCameraByCode(event.getCameraId());
            if (camera != null){
                workorder.setOrderAreaid(camera.getCameraAreaid());//所属城区
                workorder.setOrderStreetid(camera.getCameraStreetid());//所属街道
                workorder.setCameraCode(camera.getCameraCode());//探头编号
                workorder.setCameraName(camera.getCameraName());//探头名称
                workorder.setOrderLatitude(camera.getCameraLatitude());//纬度
                workorder.setOrderLongitude(camera.getCameraLongitude());//经度
                workorder.setOrderAddress(camera.getCameraAddress());//地址
            }
        }
        if (StrUtils.isNotNull(event.getSceneId())) {
            List<DbSceneEvent> sceneEventList = dbSceneEventService.querySceneEventBySceneCode(event.getSceneId(), "数字城管");
            if (sceneEventList != null && sceneEventList.size() > 0) {
                DbSceneEvent sceneEvent = sceneEventList.get(0);
                workorder.setOrderType(sceneEvent.getOrderType());//问题类型
                workorder.setOrderBigType(sceneEvent.getOrderBigtype());//问题大类
                workorder.setOrderSmallType(sceneEvent.getOrderSmalltype());//问题小类
                if (StrUtils.isNotNull(sceneEvent.getEventDescription())){
                    workorder.setOrderDescription(sceneEvent.getEventDescription());//问题描述
                }
                workorder.setOrderStatus(eventStatusVo.getOrderStatus());//案卷状态  待审核状态
                workorder.setOrderPushStatus(eventStatusVo.getOrderPushStatus());//案卷状态  待审核状态
                //workorder.setOrderPushStatus(StatusEnum.PUSH_DAI.getStatusId());//推送状态
                if (StrUtils.isNotNull(sceneEvent.getOrderType())) {
                    workorder.setEventType(eventTypeService.queryEventTypeNameByCode(sceneEvent.getOrderType()));
                }
                if (StrUtils.isNotNull(sceneEvent.getOrderBigtype())) {
                    workorder.setEventBigType(eventTypeService.queryEventTypeNameByCode(sceneEvent.getOrderBigtype()));
                }
                if (StrUtils.isNotNull(sceneEvent.getOrderSmalltype())) {
                    workorder.setEventSmallType(eventTypeService.queryEventTypeNameByCode(sceneEvent.getOrderSmalltype()));
                }
                imageNum = sceneEvent.getEventImageNum();
            }
        }
        Long orderId = dbMonitorWorkorderService.editMonitorWorkorder(workorder, event.getCreatedBy());
        //判断是否需要审核
        if (eventStatusVo.getExamine()){//需要审核
            //审核信息
            DbMonitorEventExamine examine = new DbMonitorEventExamine();
            examine.setEventId(workorder.getEventId());//事件编号
            examine.setExamineStatus(eventStatusVo.getExamineStatus());//审核状态
            examine.setExamineUser("sys");//审核人
            examine.setExamineTime(LocalDateTime.now());//审核时间
            dbMonitorEventExamineService.examineEvent(examine,"sys");
        }

        //查询事件图片
        List<DbAiEventImage> aiEventImageList = dbAiEventImageService.getAiEventImageByEventId(event.getId(),null);
        List<DbMonitorWorkorderImage> images = new ArrayList<>();
        for (DbAiEventImage dbAiEventImage : aiEventImageList) {
            if (imageNum != null && images.size() >= imageNum){
                break;
            }
            DbMonitorWorkorderImage image = new DbMonitorWorkorderImage();
            image.setOrderId(orderId);
            image.setFileTime(dbAiEventImage.getInterceptTime());
            image.setFileUrl(dbAiEventImage.getImageUrl());
            image.setMediaType(dbAiEventImage.getImageType());
            image.setFileType("report");
            images.add(image);
         }
        //编辑工单图片信息
        dbMonitorWorkorderImageService.editWorkorderImage(orderId, images, event.getCreatedBy());
        return orderId;
    }

}
